<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\logic\BrandLogic;
use think\facade\Request;
use think\facade\View;

class Brand extends BaseController
{
    public function brandList()
    {
        //处理where条件拼装

        list($where,$whereOr) = BrandLogic::getWhere();

        //查询数据

        list($list_brandList,$page)=\app\admin\model\Brand::getBrandList($where,$whereOr);

        //赋值
        View::assign('list',['list'=>$list_brandList,'page'=>$page]);
        //渲染

        if (Request::isPost()){
            $post=Request::post();
            $img=Brand::upload('img','brand_img','md5');
            if (!empty($img)){
                $post['brand_logo']=$img;
            }
            $add=\app\admin\model\Brand::create($post);
            if ($add){
                $this->success('添加成功',url('Brand/brandList'));
            }else{
                $this->error('网络波动,添加失败,请重试');
            }

        }
        return view();

    }

    public function brandEdit()
    {
        $id=Request::get('id','');
        $value=\app\admin\model\Brand::where('brand_id',$id)->find();
        View::assign('value',$value);
        if (Request::isPost()){
            $post=Request::post();
            $img=Brand::upload('img','brand_img','md5');
            if (!empty($img)){
                $post['brand_logo']=$img;
            }
            $edit=\app\admin\model\Brand::where('brand_id',$id)->update($post);

            if ($edit){
                $this->success('修改成功',url('Brand/brandList'));
            }else{
                $this->error('修改失败');
            }
        }
        return view();
    }

    public function brandDel()
    {
        $id=Request::get('id','');
        $del=\app\admin\model\Brand::destroy($id);
        if ($del){
            $this->success('删除成功',url('Brand/brandList'));
        }else{
            $this->error('网络波动,删除失败');
        }
    }

    public function brandRedel()
    {
        $id=Request::get('id','');
        if ($id!=''){
            $restore=\app\admin\model\Brand::onlyTrashed()->find($id);
            $restore->restore();
            if ($restore){
                $this->success('还原成功',url('Brand/brandList'));
            }else{
                $this->error('网络波动,还原失败');
            }
        }
        $list=\app\admin\model\Brand::onlyTrashed()->select();
        View::assign('list',$list);
        return view();
    }
}